📜 ประวัติการใช้งาน (Audit Log)
บันทึกทุกกิจกรรมในระบบตามมาตรฐาน · 136 events · 15 modules · 5 severity levels · Immutable INSERT-only
| เวลา | ระดับ | Event ID | ผู้กระทำ | ข้อความ | IP |
|---|
| Event ID | Module | Entity | Action | Severity | ข้อความ | Diff | Retention |
|---|
📐 หลักการบันทึก Audit Trail · 10 ข้อ
ทุก event ที่ระบบบันทึก ต้องเป็นไปตามหลักการเหล่านี้ — Source of Truth ในการออกแบบทุก API endpoint
⚡ ระดับความรุนแรง (Severity) · 5 ระดับ
กำหนดความสำคัญและ retention · SECURITY/CRITICAL trigger alert
| ระดับ | การใช้งาน | Alert | เก็บนาน |
|---|
⏳ นโยบาย Retention
ระยะเวลาเก็บรักษา log ตามประเภท + กฎหมาย PDPA
🔬 Field Tracking · ฟิลด์ที่เก็บ before/after diff
เฉพาะฟิลด์ที่อยู่ในรายการนี้เท่านั้นที่จะถูกบันทึก diff ลงใน changes JSONB · ฟิลด์ PII (password, OTP, credit card) ถูกแยกออกเสมอ
🔌 API Endpoint สำหรับบันทึก Audit Log
ทุกหน้าจอเรียก endpoint นี้หลัง action ที่ต้อง audit — fire-and-forget แบบ async · server validate + persist + (option) trigger alert
🗄️ PostgreSQL Schema · ตาราง audit_logs
ทุก audit event = 1 row ใน audit_logs · INSERT-only · trigger บล็อก UPDATE/DELETE
| Column | Type | Required | Notes |
|---|---|---|---|
| id | BIGSERIAL | ✓ | PK auto |
| org_id | UUID | ✓ | Tenant scope · FK organizations |
| user_id | UUID | optional | Actor · NULL สำหรับ system events |
| event_id | VARCHAR(20) | ✓ | เช่น AUTH-001, PRJ-007 |
| severity | VARCHAR(20) | ✓ | INFO / AUDIT / SECURITY / WARNING / CRITICAL |
| entity_type | VARCHAR(40) | ✓ | เช่น 'project', 'task', 'ncr' |
| entity_id | UUID | ✓* | * ยกเว้น system events |
| action | VARCHAR(60) | ✓ | เช่น 'project.close' |
| message_th | TEXT | ✓ | Render จาก template ตอน insert |
| message_en | TEXT | ✓ | Render จาก template |
| changes | JSONB | update only | before/after diff · ห้ามเก็บ PII |
| reason | TEXT | CRITICAL only | User-provided reason |
| ip_address | INET | ✓ | ใช้ x-forwarded-for ถ้าผ่าน proxy |
| user_agent | TEXT | ✓ | truncate 500 chars |
| session_id | UUID | ✓* | * ยกเว้น system |
| occurred_at | TIMESTAMPTZ | ✓ | UTC · DEFAULT NOW() |
📦 SDK Helper · auditLog() — ใช้ทุกที่
Helper ที่ wrap API endpoint · จัดการ session, IP, user-agent ให้อัตโนมัติ · auto-render TH/EN message
🛡️ Security & Compliance Rules
- Immutable: trigger PostgreSQL block ทุก UPDATE/DELETE บน
audit_logs· ใช้ partition by month เพื่อ archive - PII redaction: middleware strip ฟิลด์
password,password_hash,otp,credit_card,tax_idจากchangesก่อน insert - Reason required: ถ้า severity = CRITICAL แต่
reasonว่างเปล่า → 422 Unprocessable - Alert pipeline: SECURITY ≥ 5/5min → email admin · CRITICAL → SMS + email admin + owner ทันที
- Index:
(org_id, occurred_at DESC),(entity_type, entity_id),(user_id, occurred_at DESC) - Visibility: User เห็นของตน · QCM/PM เห็น project ที่ตน manage · Admin เห็นทุก log ของ org · Compliance officer ข้าม tenant ได้